package vip.meeet.util;

import vip.meeet.jdbc.ConnectionHelper;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class SimplifiedAndTranditional {

    public static final HashMap<Character,Character> map = new HashMap<>();

    static {
        try {
            jf();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public static void jf() throws SQLException, ClassNotFoundException {
        Connection connection = ConnectionHelper.getConnection("material", "tim", "123456");
        String sql = "select original,complex from jf";
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery(sql);
        StringBuffer sb = new StringBuffer();
        while (rs.next()){
            map.put(rs.getString(2).charAt(0),rs.getString(1).charAt(0));
        }
    }

    public static String tranditional2Simplified(String tranditional){
        Character rs;
        StringBuffer sb = new StringBuffer();
        for(int i=0;i<tranditional.length();i++){
            char c = tranditional.charAt(i);
            rs = map.get(c);
            if(rs == null){
                sb.append(c);
//                System.out.println(c);
            }else{
                sb.append(rs);
            }
        }
        return sb.toString();
    }
}
